package gavdi.dk.view;




import gavdi.dk.controller.LoginController;


import gavdi.dk.util.Tools;
import gavdi.dk.view.R;


import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.ImageButton;
import android.widget.EditText;

public class LoginActivity extends Activity {
	private ImageButton btnLogin;
	private EditText txtUserID;
	private Bundle bundle;


	@Override
    public void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);
        
        // Its better to implement AsyncTask
        setStrictPolicy();
        
        // Initialize all attributes
        initAttributes();
        
        txtUserID.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable s) {
				
				if ( s.length() == 8)
				{

					
					long start = System.currentTimeMillis();
				//	Controller.createTestData(getBaseContext());
					long total = (System.currentTimeMillis()-start);
					System.out.println("Det tog: " + ( total / 1000 ) + " sekunder at skabe alt test data");
			

					
					// Create a new Intent, that will go to MenuActivity.
					Intent intent = new Intent(getBaseContext() ,MenuActivity.class);
					
					// Starts the entire Login Process, and returns a bundle when its done.
					
		
					if(Tools.isInternetOn(getBaseContext())) {
						 
						// INTERNET IS AVAILABLE, DO STUFF..
						LoginController.startLoginProcess(s, getBaseContext());


						}else{
						 
						// NO INTERNET AVAILABLE, DO STUFF..
						 
						 
						}
					
					bundle.putString("userID", s.toString());
					
					// Puts the bundle on the Inten
					intent.putExtras(bundle);
					
					// Resets the TextView, where the user typed his ID.
					txtUserID.setText("");
					
					// Starts the activity, with the given Intent.				
					startActivity(intent);	

				

			}}

			
			


			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			public void onTextChanged(CharSequence s, int start, int before,int count) {
				// TODO Auto-generated method stub
				
			}
		});

        
        
    }
    private void setStrictPolicy(){
    	if (android.os.Build.VERSION.SDK_INT > 9) {
    		  StrictMode.ThreadPolicy policy = 
    		    new StrictMode.ThreadPolicy.Builder().permitAll().build();
    		  StrictMode.setThreadPolicy(policy);
    		}
    }
	
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.login_layout, menu);
		return true;
	}

	private void initAttributes() {
		btnLogin = (ImageButton) findViewById(R.id.btnLogin);
		txtUserID = (EditText) findViewById(R.id.txtUserID);
		bundle = new Bundle();
	}
	

	


}
